import pymysql


def con_database():
    conn = pymysql.connect(
        host='127.0.0.1',
        port=3306,
        user='root',
        password='heyong728350858',
        database='blog',
        charset='utf8mb4',
    )
    cur = conn.cursor()
    return conn, cur


def close_conn(conn, cur):
    cur.close()
    conn.close()


def change_db(sql):
    conn, cur = con_database()
    try:
        cur.execute(sql)
        conn.commit()
        return True
    except:
        conn.rollback()
        return False
    finally:
        close_conn(conn, cur)


def select_db(sql):
    conn, cur = con_database()
    try:
        cur.execute(sql)
        return cur.fetchall()
    except:
        conn.rollback()
        return False
    finally:
        close_conn(conn, cur)


if __name__ == '__main__':
    sql = 'insert into user values(null,"heyong","heyong","woaileimu@qq.com");'
    conn, cur = con_database()
    try:
        cur.execute(sql)
        conn.commit()
    except:
        conn.rollback()
    finally:
        close_conn(conn, cur)
